class Valleyers::Show::Albums::Show::PhotosController < ApplicationController
  protect_from_forgery :except => :create

  def index
    @photo = Photo.find params[:photo_id]
    #初始化相册评论
    @post = Post.new :album_id => params[:album_id],
      :created_by_valleyer_id => current_valleyer_id
  end

  def new
    @photo = Photo.new
    if params[:album_id].nil?
      @album_name = "未知相册#{(Album.find(:all,
        :conditions => ["name like ? and author_id = ?",
        "%未知相册%", current_valleyer_id]).length + 1)}"
    else
      @album_name = Album.find(params[:album_id]).name
    end
  end

  def create
    @valleyer = Valleyer.find params[:author_id]
    @photo = Photo.new :image_file => params[:Filedata],
        :author => @valleyer
    @album = Album.find_by_name_and_author_id(params[:album_name], @valleyer.id)
    if @album.nil?
      @photo.album = Album.new :name => params[:album_name],
        :remark => "没有命名的相册",
        :author => @valleyer,
        :documet_type => DocumetType.find_by_name("未分类")
      @photo.album.save
    else
      @photo.album = @album
    end
    render :text => @photo.save ? '成功':'失败'
  end

  def show
    @album = Album.find_by_name_and_author_id(params[:album_name], current_valleyer_id)
    @photo = Photo.first(
      :conditions => ['album_id = ?', @album.id],
      :order => 'created_at desc'
    )
  end

  def edit
    @photo = Photo.find params[:id]
  end

  def update
    @photo = Photo.find params[:id]
    render :edit unless @photo.update_attributes params[:photo]
    #初始化相册评论
    @post = Post.new :album_id => params[:album_id],
      :created_by_valleyer_id => current_valleyer_id
  end

  def authorize

  end
end
